package cc.zy.base.businesses.service.impl;

import cc.zy.base.businesses.entity.Notice;
import cc.zy.base.businesses.entity.NoticeUser;
import cc.zy.base.businesses.mapper.NoticeMapper;
import cc.zy.base.businesses.service.INoticeService;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 通知表 Service实现
 *
 * @author 杨东豪
 * @date 2021-01-26
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    private final NoticeMapper noticeMapper;

    @Override
    public IPage<Notice> findNotices(QueryRequest request, Notice notice) {
        Page<Notice> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countNoticeDetail(notice));
        return this.baseMapper.findNoticeDetailPage(page, notice);
    }

    @Override
    public IPage<Notice> findNoticesDetailList(Notice notice, QueryRequest request) {
        if (notice != null && notice.getTitle() != null) {
            notice.setTitle(notice.getTitle().trim());
        }
        Page<Notice> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countNoticeDetail(notice));
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return this.baseMapper.findNoticeDetailPage(page, notice);
    }

    @Override
    public List<Notice> findNotices(Notice notice) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createNotice(Notice notice) {
        this.save(notice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNotice(Notice notice) {
        this.saveOrUpdate(notice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNotice(Notice notice) {
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        this.remove(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNoticeById(Integer noticeId) {
        baseMapper.deleteById(noticeId);
    }

    @Override
    public List<Notice> findCreatIdByUserId(Integer noticeId) {
        return baseMapper.findCreatIdByUserId(noticeId);
    }

    @Override
    public List<Integer> findUserIdListByUserId(Integer noticeId) {
        return baseMapper.findUserIdsByUserId(noticeId);
    }

    @Override
    public List<Integer> findUserIdListByUserIdPool(Integer noticeId) {
        return baseMapper.findUserIdsByUserIdPool(noticeId);
    }

    @Override
    public Notice findNoticeById(Integer noticeId) {
        return baseMapper.findNoticeById(noticeId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertNoticeUser(NoticeUser noticeUser) {
        noticeMapper.insertNoticeUser(noticeUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNoticeByidy(Notice notice) {
        return noticeMapper.updateNoticeByidy(notice);
    }

    @Override
    public long wxNoticeListCount(String userId, String identity, Integer batchId, Integer studyTypeId, String tel) {
        return noticeMapper.wxNoticeListCount(userId, identity, batchId, studyTypeId, tel);
    }

    @Override
    public List<Notice> wxNoticeListPage(String userId, String identity, Integer batchId, Integer studyTypeId, String tel, Integer startNumber, Integer endNumber) {
        return noticeMapper.wxNoticeListPage(userId, identity, batchId, studyTypeId, tel, startNumber, endNumber);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNoticeReadStatus(String userId, Integer msgId, String identity, Integer batchId, Integer studyTypeId, String tel, String stuFlag) {
        return noticeMapper.updateNoticeReadStatus(userId, msgId, identity, batchId, studyTypeId, tel, stuFlag);
    }

    @Override
    public List<NoticeUser> findShareNoticeIsShow(String noticeId, String stuFlag, String openId) {
        return noticeMapper.findShareNoticeIsShow(noticeId, stuFlag, openId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateShareNoticeReadStatus(String openId, Integer msgId, String stuFlag) {
        return noticeMapper.updateShareNoticeReadStatus(openId, msgId, stuFlag);
    }
}
